<?php

namespace app\common\command;
use addons\shopro\service\Wallet as WalletService;
use app\admin\model\shopro\user\WalletLog;
use app\common\model\shopro\ReleaseAccelerateLogModel;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\exception\HttpResponseException;

class Tuijianrelease extends Command
{
    protected function configure()
    {
        $this->setName('tuijianrelease')->setHelp('推荐加速释放')->setDescription('推荐加速释放');

    }

    protected function execute(Input $input, Output $output)
    {
        $is_release = config('site.is_release');
        if ($is_release == 1) {
            ini_set('memory_limit', "-1");
            $accelerateLogModel = new ReleaseAccelerateLogModel();
            $count              = $accelerateLogModel->whereTime('createtime', 'd')->count();
//            $count = 0;
            if(config('site.tiaoshi') == 1){
                $count = 0;
            }
            if (!$count) {
                $walletLogModel = new WalletLog();
                $accelerate_max = config('site.accelerate_max');
                $walletLogInfo = $walletLogModel->where(['event'   => 'release', 'type'    => 'score'])->whereTime('createtime', 'd')->select();
                $inser          = [];
                if ($walletLogInfo) {
                    Db::startTrans();
                    try {
                        $sumScoresOne = [];
                        $sumScoresTwo = [];
                        $accelerate_one = config('site.accelerate_one') / 100;
                        $accelerate_all = config('site.accelerate_all') / 100;
                        foreach ($walletLogInfo as $k=>$v){
                            if(isset($v['ext']['parent_user_id']) && $v['ext']['parent_user_id'] > 0){
                                //分享释放
                                $parent_user_id = $v['ext']['parent_user_id'];
                                $amount = abs($v['amount']);
                                if(isset($sumScoresOne[$v['ext']['parent_user_id']])){
                                    $sumScoresOne[$v['ext']['parent_user_id']] += $amount;
                                }else{
                                    $sumScoresOne[$v['ext']['parent_user_id']]  = $amount;
                                }
                                //团队加速
                                $children_level =  db('user_children')->where(['member_id'=>$parent_user_id,'level'=>1])->count();
                                if($children_level > 1){
                                    if($children_level >= $accelerate_max){
                                        $children_level = $accelerate_max;
                                    }
                                    if(isset($tow_arr[$parent_user_id]['children_id'])){
                                        $cj_amount = $walletLogModel->whereIn('user_id', $tow_arr[$parent_user_id]['children_id'])->where(['event'   => 'release', 'type'    => 'score'])->whereTime('createtime', 'd')->sum('amount');
                                        if($cj_amount){
                                            $cj_amount = abs($cj_amount);
                                            $sumScoresTwo[$parent_user_id] = $cj_amount;
                                        }
                                    }else{
                                        $tow_arr[$parent_user_id]['children_id'] = db('user_children')->where(['member_id'=>$parent_user_id])->whereBetween('level', [2,$children_level])->column('children_id');
                                    }
                                }
                            }
                        }
                        //分享释放
                        if ($sumScoresOne) {
                            $inser = $this->accelerateshifang($sumScoresOne, $accelerate_one,'zhi_release');
                            if ($inser['result']) {
                                unset($inser['result']);
                                $accelerateLogModel->create($inser);
                            }
                        }
                        //团队加速
                        if ($sumScoresTwo) {
                            $inser = $this->accelerateshifang($sumScoresTwo, $accelerate_all,'cengji_release');
                            if ($inser['result']) {
                                unset($inser['result']);
                                $inser['shifang_type'] = 2;
                                $accelerateLogModel->create($inser);
                            }
                        }
                        Db::commit();
                    } catch (HttpResponseException $e) {
                        Db::rollback();
                        $data = $e->getResponse()->getData();
                        $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
                        format_log_error($e, 'Release.release.HttpResponseException', $message);
                    }
                    if ($inser) {
                        echo "释放成功";
                    }
                } else {
                    echo "无会员满足释放";
                }
            } else {
                echo "今日已释放";
            }
        }
    }

    /**
     * @menu 释放
     * @Author:男人的坏
     * @Date:2024-04-16 16:49
     */
    public function accelerateshifang($data, $accelerate,$event)
    {
        $golen_scale = config('site.golen_scale') / 100;        //金豆比例
        $slive_scale = (100 - config('site.golen_scale')) / 100;//银豆比例
        $userModel   = new \app\common\model\User();
        $result      = false;
        $all_score   = 0;
        $all_golden  = 0;
        $all_silver  = 0;
        $user_num    = 0;
        $user_ids    = '';
        if($event == 'zhi_release'){
            $memo = '分享释放';
        }else{
            $memo = '团队加速';
        }
        foreach ($data as $k => $v) {
            $userInfo = $userModel->where(['id' => $k])->field('id,score,is_old')->find();
            if($userInfo){
                if($userInfo->score > 0){
                    $release  = bcmul($v, $accelerate, 2);
                    if ($userInfo->score <= $release) {
                        $release = $userInfo->score;
                    }
                    $all_score += $release;
                    $user_num++;
                    $user_ids   .= ','.$k;
                    $result     = WalletService::change($k, 'score', -$release, $event, [],
                        $memo);
                    $golden     = bcmul($release, $golen_scale, 2);
                    $all_golden += $golden;
                    $result     = WalletService::change($k, 'golden', $golden, $event, [],
                        $memo.'积分转化金豆');
                    $slive      = bcmul($release, $slive_scale, 2);
                    $all_silver += $slive;
                    $result     = WalletService::change($k, 'silver', $slive, $event, [],
                        $memo.'积分转化银豆');
                }
            }
        }
        $arr['result']     = $result;
        $arr['user_ids']   = trim($user_ids,',');
        $arr['user_num']   = $user_num;
        $arr['all_golden'] = $all_golden;
        $arr['all_silver'] = $all_silver;
        $arr['all_score']  = $all_score;
        return $arr;
    }
}
